package com.jxc.wiki.service.impl;

import com.jxc.wiki.entity.Comment;
import com.jxc.wiki.entity.CommentExample;
import com.jxc.wiki.interceptor.LoginInterceptor;
import com.jxc.wiki.mapper.CommentMapper;
import com.jxc.wiki.mapper.EbookSnapshotMapperCust;
import com.jxc.wiki.request.CommentSaveParamReq;
import com.jxc.wiki.response.CommentQueryParamResp;
import com.jxc.wiki.response.StatisticParamCommentResp;
import com.jxc.wiki.service.CommentService;
import com.jxc.wiki.util.CopyClassOrListUtil;
import com.jxc.wiki.util.SnowFlakeUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {

    private static final Logger LOG = LoggerFactory.getLogger(LoginInterceptor.class);

    @Resource
    EbookSnapshotMapperCust ebookSnapshotMapperCust;

    @Resource
    CommentMapper commentMapper;

    @Resource
    SnowFlakeUtil snowFlakeUtil;

    // 评论保存业务层方法
    public void saveComment(CommentSaveParamReq req){
        LOG.info("CommentSaveReq的内容：{}",req);
        Comment comment = CopyClassOrListUtil.copy(req,Comment.class);
        LOG.info("Comment的内容：{}",comment);
        if (ObjectUtils.isEmpty(req.getId())){

            // 如果id的值为空，则用雪花算法生成id，并设置为comment的id
            comment.setId(snowFlakeUtil.nextId());
            commentMapper.insert(comment);
        }
        if (ObjectUtils.isEmpty(req.getAvatar())){
            // 如果头像的值为空，手动添加本地github头像地址
            comment.setAvatar("/image/github_Cover.jpg");
        }
        if (ObjectUtils.isEmpty(req.getCreateTime())){
            // 如果日期为空，手动添加日期
            Date date = new Date();
            comment.setCreateTime(date);
        }
//        CommentExample commentExample = new CommentExample();
//        CommentExample.Criteria criteria = commentExample.createCriteria();


    }

    // 根据 docName 获取评论
    public List<CommentQueryParamResp> getComment(String docName){

        LOG.info("后端业务层方法getComment接受到的请求参数：{}",docName);
        // 创建查询模板
        CommentExample commentExample = new CommentExample();
        CommentExample.Criteria criteria = commentExample.createCriteria();
        // 根据 docName 获取对应文章的评论
        criteria.andDocNameEqualTo(docName);
        List<Comment> commentList = commentMapper.selectByExample(commentExample);
        LOG.info("commentList的内容：{}",commentList);
        // 先用工具类拷贝查询出来的Comment类型列表到 CommentQueryResp类型的列表
        List<CommentQueryParamResp> commentQueryRespList = CopyClassOrListUtil.copyList(commentList, CommentQueryParamResp.class);
        LOG.info("commentQueryRespList的内容：{}",commentQueryRespList);

        // 返回CommentQueryResp类型的数组给接口
        return commentQueryRespList;
    }

    // 获取评论总数和今日评论数
    public StatisticParamCommentResp getStatisticComment(){
        Integer countToday = ebookSnapshotMapperCust.getStatisticCommentToday();
        Integer countTotal = ebookSnapshotMapperCust.getStatisticCommentTotal();
        StatisticParamCommentResp statisticParamCommentResp = new StatisticParamCommentResp();
        statisticParamCommentResp.setTodayComment(countToday);
        statisticParamCommentResp.setTotalComment(countTotal);
        return statisticParamCommentResp;
    }
}
